package com.usc.flink.datestream_api.window;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;

/**
 * @author jianjianDuan
 * @date 7/6/2020 3:42 PM
 */
public class SocketWindowWordCount {
    public static void main(String[] args) throws Exception {

        // 入口类: 1.设置参数 2.创建数据源 3.提交任务
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        // 创建本地端口读取数据源 DataStream定义了非常多的常见操作(如: 过滤、转换、聚合、窗口、关联等)
        DataStream<String> text = env.socketTextStream("localhost", 9999);

        // 将读取到所有数据进行flatmap
       /* DataStream<Tuple2<String, Integer>> wordCounts = text.flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {
            @Override
            public void flatMap(String value, Collector<Tuple2<String, Integer>> out) throws Exception {
                for (String word : value.split("\\s")) {
                    out.collect(Tuple2.of(word, 1)); // 也可以 out.collect(new Tuple2<>(word, 1));

                }
            }        });*/

        DataStream<Tuple2<String, Integer>> wordCounts = text.flatMap((String value, Collector<Tuple2<String, Integer>> out) -> {
                for (String word : value.split("\\s")) {
                    out.collect(Tuple2.of(word, 1)); // 也可以 out.collect(new Tuple2<>(word, 1));
                }
            }
        ).returns(Types.TUPLE(Types.STRING, Types.INT));

        // 对key-0 即word进行分组求和
        DataStream<Tuple2<String, Integer>> windowCounts = wordCounts
                .keyBy(0)
                .timeWindow(Time.seconds(5))
                .sum(1); // 对wordCounts的1号字段进行求和

        windowCounts.print().setParallelism(1);
        env.execute("Socket Window WordCount");

    }
}
